package screens;

import log.Logger;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.BasicEditField;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import runnables.LoginRunnable;
import strings.WebcampusResource;
import util.LookAndFeel;

import com.rim.samples.device.push.PushController;

import customfields.ButtonUADE;
import customfields.TextBoxField;
import customfields.WhiteLabelField;

public class ConfigurationScreen extends MainScreen implements WebcampusResource{

	// RESOURCES
	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	// ATTRIBUTES
	private VerticalFieldManager vfm;

	private BasicEditField user;
	private PasswordEditField password;
	private ButtonUADE entrar;

	//CONSTRUCTOR
	public ConfigurationScreen() {
		super(MainScreen.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR);

		//TODO etapa 3: comment for etapa 3: remove log screen access
//		MenuItem menu_log = Menues.getLogMenuItem("LOG", 600, 1);
//		addMenuItem(menu_log);

		// main manager and background
		vfm = new VerticalFieldManager(Field.USE_ALL_HEIGHT );
		Bitmap background_bitmap = Bitmap.getBitmapResource(LookAndFeel.background_img);
		Bitmap background_bitmap_resized = new Bitmap(Display.getWidth(), Display.getHeight());
		background_bitmap.scaleInto(background_bitmap_resized, Bitmap.FILTER_LANCZOS | Bitmap.SCALE_TO_FILL);
		Background background = BackgroundFactory.createBitmapBackground(background_bitmap_resized);
		vfm.setBackground(background);

		// Header
		Bitmap header_img = Bitmap.getBitmapResource(LookAndFeel.header_img);
		Bitmap header_img_resized = new Bitmap(Display.getWidth(), (int) (Display.getHeight()*0.25));
		header_img.scaleInto(header_img_resized, Bitmap.FILTER_LANCZOS, Bitmap.SCALE_TO_FILL);
		BitmapField header = new BitmapField(header_img_resized,Field.FIELD_HCENTER);
		vfm.add(header);

		// Separator
		vfm.add(new SeparatorField());
		vfm.add(new LabelField(""));

		WhiteLabelField lblUser = new WhiteLabelField(resources.getString(USERNAME_LABEL),Field.FIELD_HCENTER);
		user = new BasicEditField("", Controller.getInstance().getUsername(), 30, BasicEditField.NO_NEWLINE );
		TextBoxField tbfUser = new TextBoxField(Display.getWidth()/2, getFont().getHeight(), Field.FIELD_HCENTER);
		tbfUser.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color, 
				LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color));
		tbfUser.add(user);

		WhiteLabelField lblPass = new WhiteLabelField(resources.getString(PASSWORD_LABEL), Field.FIELD_HCENTER);
		password = new PasswordEditField("", Controller.getInstance().getPassword(), 30,PasswordEditField.NO_NEWLINE);
		TextBoxField tbfPass = new TextBoxField(Display.getWidth()/2, getFont().getHeight(), Field.FIELD_HCENTER);
		tbfPass.setBackground(BackgroundFactory.createLinearGradientBackground(LookAndFeel.button_upper_color,
				LookAndFeel.button_upper_color, LookAndFeel.button_lower_color, LookAndFeel.button_lower_color));
		tbfPass.add(password);

		entrar = new ButtonUADE(resources.getString(LOGIN_BUTTON),Field.FIELD_HCENTER );
		entrar.setChangeListener(buttonListener);

		vfm.add(lblUser);
		vfm.add(tbfUser);
		vfm.add(new LabelField(""));
		vfm.add(lblPass);
		vfm.add(tbfPass);
		vfm.add(new LabelField(""));
		vfm.add(entrar);
		vfm.add(new LabelField(""));

		add(vfm);	
	}

	// dialogs

	public static void showError(String message){
		Dialog.alert(message);
	}

	// LISTENERS
	private FieldChangeListener buttonListener = new FieldChangeListener() {
		public void fieldChanged(Field field, int context) {
			String u = user.getText();
			String p = password.getText();
			if (u.equalsIgnoreCase("") || p.equalsIgnoreCase("")){
				Dialog.alert (resources.getString(ALERT_MUSTUSERPASS));
			}
			else {
				Controller.getInstance().saveLogin(u, p);
				PersistentStore.destroyPersistentObject(0xc348b0df1b6cb526L);
				Logger.log("Persistent object destroyed");
				try{
					LoginRunnable doLogin = new LoginRunnable();
					PleaseWaitPopupScreen.showScreenAndWait(doLogin, resources.getString(POPUP_LOGIN));
					// etapa 3: uncomment for etapa 3
					PushController.register();
				} catch (Exception e){
					//TODO check this exception
					log.Logger.log("login exception caught)");
					//Dialog.inform("Error");
				}
			}
		}
	};


	protected boolean onSavePrompt() {
		return true;
	}

}
